#include "chuangxin.h"
#include <iostream>
#include <cstring>

chuangxin::chuangxin()
{
}
void chuangxin::testLE()
{
    union A{
        int i;
        char c;
    };
    union A a;
    a.i=10;
    if(a.c==10)
    {
        std::cout<<"little-endian"<<std::endl;
    }
    else
    {
        std::cout<<"big-endian"<<std::endl;
    }
}
bool chuangxin::findBinary(int *A, int n, int data, int &ind)
{
    int low=0;
    int high=n-1;
    while(low<=high)
    {
        int mid=(low+high)/2;
        if(A[mid]==data)
        {
            ind=mid;
            return true;
        }
        else if(A[mid]>data)
        {
            high=mid-1;
        }
        else
        {
            low=mid+1;
        }
    }
    ind=low;
    return false;
}
void chuangxin::fillK(const char *pt, int *A, int n)
{
    A[0]=-1;
    int i=0;
    int j=-1;
    while(i<n)
    {
        if(j==-1 || pt[i]==pt[j])
        {
            i++;
            j++;
            A[i]=j;
        }
        else
        {
            j=A[j];
        }
    }
}
int chuangxin::findKMP(const char *src, const char *pt, int *A)
{
    int mlen=strlen(pt);
    int srclen=strlen(src);
    int i=0,j=0;
    while(i<srclen)
    {
        if(j==-1 || src[i]==pt[j])
        {
            i++;
            j++;
        }
        else
        {
            j=A[j];
        }
    }
    if(j==mlen)
        return (i-j);
    else
        return -1;
}
